/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.MyCrudService;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.sys.dao.AddTenementInfoDao;
import com.thinkgem.jeesite.modules.sys.dao.AgentTenementDao;
import com.thinkgem.jeesite.modules.sys.dao.CommunityMapper;
import com.thinkgem.jeesite.modules.sys.entity.TbCommunity;
import com.thinkgem.jeesite.modules.sys.entity.TbUserTenementInfo;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.ConstraintViolationException;
import java.math.BigDecimal;
import java.util.List;

/**
 * 提现Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
public class AddTenementInfoService  {
	@Autowired
	private AddTenementInfoDao addTenementInfoDao;
	@Autowired
	private CommunityMapper communityMapper;
	@Autowired
	private AgentTenementDao agentTenementDao;


	//查询小区名称
	public List<TbCommunity> findCommityList(){
		return addTenementInfoDao.findCommityList();
	}

	public String importTenement(List<TbUserTenementInfo> list,TbCommunity tbCommunity){
		int successNum = 0;
		int failureNum = 0;
		StringBuilder failureMsg = new StringBuilder();
		for (TbUserTenementInfo tbUserTenementInfo : list){
			TbUserTenementInfo tb=agentTenementDao.getByMobile(tbUserTenementInfo.getMobile());
			if(tb!=null){
				failureMsg.append("<br/>业主姓名 "+tbUserTenementInfo.getName()+" 导入失败：该手机号已经存在！");
			}else{
				try{
					tbUserTenementInfo.setCommunityId(tbCommunity.getId());
					tbUserTenementInfo.setUid(UserUtils.getUser().getId());
					tbUserTenementInfo.setName(tbUserTenementInfo.getName());
					tbUserTenementInfo.setBuildingNo(tbUserTenementInfo.getCoefficient().setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
					//返还消费系数
					addTenementInfoDao.insertTbUserTenementInfo(tbUserTenementInfo);
					successNum++;
				}catch(ConstraintViolationException ex){
					//failureMsg.append("<br/>业主姓名 "+tbUserTenementInfo.getName()+" 导入失败：");
					List<String> messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
				/*	for (String message : messageList){
						failureMsg.append(message+"; ");*/
						failureNum++;
				//	}
				}catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		}
		if (failureNum>0){
			failureMsg.insert(0, "，失败 "+failureNum+" 条物业信息！");
		}else{
			failureMsg.append("<br/>已成功导入 "+successNum+" 条物业信息");
		}
		return failureMsg.toString();
		//addMessage(redirectAttributes, "已成功导入 "+successNum+" 条物业信息"+failureMsg);
	}

	//添加或修改
	public  void saveTenementInfo(TbUserTenementInfo tbUserTenementInfo){
		String uid = UserUtils.getUser().getId();
		tbUserTenementInfo.setUid(uid);
		if(tbUserTenementInfo.getId()!=null){
			addTenementInfoDao.updateUserTenement(tbUserTenementInfo);
		}else{
			addTenementInfoDao.insertTbUserTenementInfo(tbUserTenementInfo);
		}

	}

	//物业信息删除
	public void deleteTenenment(TbUserTenementInfo tbUserTenementInfo){
		addTenementInfoDao.deleteTenenment(tbUserTenementInfo);
	}

	//根据id查询
	public  TbUserTenementInfo getById(TbUserTenementInfo tbUserTenementInfo){
		return addTenementInfoDao.getById(tbUserTenementInfo);
	}
	public static double round(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}
